package org.softwarepoets.emf.ecore.editor.extensions;

import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.ui.PlatformUI;

public class CreateVolatileEAttribute extends AbstractExtensionAction {

	public CreateVolatileEAttribute() {
		super(EClass.class);
	}

	@Override
	public void run(IAction action) {
		InputDialog dialog = new InputDialog(
				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
				"Enter attribute name",
				"Enter the name of the attribute  using valid java naming conventions.",
				"", new JavaNamingValidator());

		if (dialog.open() == InputDialog.OK) {
			EClass object = (EClass) element;
			String name = dialog.getValue();
			EditingDomain domain = AdapterFactoryEditingDomain
					.getEditingDomainFor(object);

			CompoundCommand cmd = new CompoundCommand();
			EAttribute attr = EcoreFactory.eINSTANCE.createEAttribute();
			attr.setName(name);
			attr.setVolatile(true);
			attr.setTransient(true);
			attr.setDerived(true);
			attr.setChangeable(false);
			cmd.append(AddCommand.create(domain, object, null, attr));
			domain.getCommandStack().execute(cmd);
		}
	}

}
